﻿using System;
using System.Linq;
using System.ServiceProcess;
using System.Threading;
using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform.Gxt;
using Biz.Process;
using Biz.Reissue;
using Hjt212Parser;
using log4net;

namespace Biz
{
    public partial class BizProcessor : ServiceBase
    {
        private static ILog Log = LogManager.GetLogger(typeof(BizProcessor));
        int milliSeconds = 1000;
        //private static string process_cns = "2011,2051,2061,2031,2072,0000,9999";
        public BizProcessor()
        {
            InitializeComponent();
        }
        //test
        public void Main()
        {
            OnStart(null);
        }
        protected override void OnStart(string[] args)
        {
            //启动各路线程
            new Thread(ProcessCmdNonHourOrDay).Start();
            new Thread(ProcessCmdHourAndDay).Start();
            new Thread(Reissue).Start();
            new Thread(new ProcessorMonthPlan4Total().GenMonthPlan4Total).Start();
            Log.Info("各路线程启动完成...");
        }
        protected override void OnStop()
        {
            Log.Error(new Exception("The processor Stoped!"));
        }

        private void ProcessCmdNonHourOrDay(object state)
        {
            while (true)
            {
                try
                {
                     CommonProcessor.ProcessNonHourOrDay();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }

        private void ProcessCmdHourAndDay(object state)
        {
            while (true)
            {
                try
                {
                    CommonProcessor.ProcessHourAndDay();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }


        private void Reissue(object state)
        {
            while (true)
            {
                try
                {
                    DataLostCmdGen.GenCmd();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(30 * 60 * 1000);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }

    }
}
